package com.wwj.springboot.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wwj.springboot.entity.CmfzGuru;
import com.wwj.springboot.service.CmfzGuruService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author wwj
 */
@Controller
@RequestMapping("guru")
public class CmfzGuruController {

    @Value("myfile-path")
    private String getPath;

    @Autowired
    private CmfzGuruService cgs;

    @RequestMapping("selectByPage")
    @ResponseBody
    public Map selectByPage(int page,int limit,String username){
        return cgs.selectByPage(page,limit,username);
    }

    @RequestMapping("addGuru")
    @ResponseBody
    public Map addGuru(CmfzGuru guru){
        return cgs.addGuru(guru);
    }


    @RequestMapping("deleteGuru")
    @ResponseBody
    public Map deleteGuru(int id){
        return cgs.deleteGuru(id);
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public CmfzGuru selectOne(int id){
        return cgs.selectOne(id);
    }


    @RequestMapping("downloadList.xls")
    public void downloadList(HttpServletResponse response){
        List<CmfzGuru> cmfzGurus =  cgs.selectAll();
        for (CmfzGuru cmfzGuru : cmfzGurus) {
            cmfzGuru.setGuruImage("E:/IDEA/Idea_Work/myFile/"+cmfzGuru.getGuruImage());
        }
        ExportParams exportParams = new ExportParams("标题","CmfzGuru");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class,cmfzGurus);

        try {
            response.setHeader("Content-Disposition",
                    "attachment;fileName="+ URLEncoder.encode(
                            "上师.xls","utf-8"
                    ));
            response.setContentType("application/vnd.ms-excel");
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("uploadList")
    @ResponseBody
    public Map uploadList(MultipartFile file) throws Exception {

        InputStream inputStream = file.getInputStream();
        /**
         * setTitleRows(1) 设置标题所占的行数
         * setHeadRows(1) 设置表头所占的行数
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setTitleRows(1);
        /**
         * 参数1 流
         * 参数2 pojoClass 普通Java对象的类对象 要导入的实体类的类对象
         * 参数3 导入参数对象
         */
        List<CmfzGuru> cmfzGurus = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);

        for (CmfzGuru cmfz : cmfzGurus){
            System.out.println(cmfz.getGuruImage()+"+++++++++000000000000000000000000000000000000000000000000000000000000000000000");
            String s = StringUtils.substringAfterLast(cmfz.getGuruImage(), "\\");
            cmfz.setGuruImage(s);
        }

        return cgs.uploadList(cmfzGurus);
    }
}
